import { tables } from "./table.mock.js";
import cmpnts from "pkgs/cmpnts";

function getFields4Check(tableName, bizData, onUpdate) {
  const table = tables.find((item) => item.name === tableName);
  if (!table) {
    throw new Error(`table ${tableName} 未找到！`);
  }
  let lst = [];
  table.fields.forEach((item) => {
    if (!bizData[item.name])
      bizData[item.name] = item.type === "boolean" ? true : "";

    let nfields = {
      label: item.title,
      name: item.name,
      data: bizData,
      fieldCmpnt: cmpnts.find(item.cmpnt).clone(),
      cmpnt: cmpnts.find("switch").clone(),
    };
    nfields.fieldCmpnt.valueKey = item.name;
    nfields.cmpnt.valueKey = item.name;
    bizData[nfields.cmpnt.valueKey] = false;
    if (!nfields.cmpnt.events) {
      nfields.cmpnt.events = {};
    }

    nfields.cmpnt.events["update:modelValue"] = function (newvalue) {
      bizData[nfields.cmpnt.valueKey] = newvalue;
      onUpdate(nfields, newvalue);
    };
    lst.push(nfields);
  });
  return lst;
}
export default {
  tables,
  getFields4Check,
};
